package com.wd.live.module_base.ui.view

import android.util.Log
import android.view.View
import androidx.viewpager.widget.ViewPager

/**
 * 作者: 王晶晶
 * 时间:2024/5/27
 * 描述:滑动
 */
class DipInTransformer: ViewPager.PageTransformer {
    private val MIN_SCALE = 0.85f
    private val mMinScale = MIN_SCALE
    private val MIN_ALPHA = 0.5f
    private val mMinAlpha = MIN_ALPHA

    override fun transformPage(page: View, position: Float) {
        Log.i("DipInTransformer", "transformPage: id = " + page.id + ", position = " + position)
        val pageWidth = page.width
        val pageHeight = page.height
        //        page.setPivotX(pageWidth * 0.5f);
        page.pivotY = pageHeight * 0.5f
        if (position < -1f) { //(-Infinity, -1]
            page.alpha = mMinAlpha
            page.scaleX = mMinScale
            page.scaleY = mMinScale
            page.pivotX = pageWidth * 1f
        } else if (position <= 1f) { //(-1, 1)
            val scaleFactor = Math.max(mMinScale, 1 - Math.abs(position))
            page.scaleX = scaleFactor
            page.scaleY = scaleFactor
            if (position < 0) {
                page.pivotX = pageWidth * (0.5f + 0.5f * scaleFactor)
            } else {
                page.pivotX = pageWidth * (0.5f - 0.5f * scaleFactor)
            }
            page.alpha = mMinAlpha + (scaleFactor - mMinScale) / (1f - mMinScale) * (1f - mMinAlpha)
        } else { //(1, +Infinity)
            page.alpha = mMinAlpha
            page.scaleX = mMinScale
            page.scaleY = mMinScale
            page.pivotX = pageWidth * 0f
        }
    }
}
